<?php
/**
 * Created by PhpStorm.
 * User: 陈艺赫
 * Date: 2017/12/12
 * Time: 4:27
 */

namespace Home\Controller;


class JylistController extends BaseController
{
    public function index()
    {
        if (empty(session('guest'))) {
            $this->success('请先登录!');
            exit;
        }
        if (IS_GET) {
            $jy = I('get.jy');
            $this->assign('jy', $jy);
        }
        $this->display();
    }

    public function dataGet()
    {
        if (IS_POST) {
            $jy = I('post.jy');
            $db_span = M('span');
            $count = $db_span->where('status = "1" and span like "%' . $jy . '%"')->count();
            $data_span = $db_span->where('status = "1" and span like "%' . $jy . '%"')->field('id')->select();
            $data_ids = array_column($data_span, 'id');
            $data_list = [];
            $i = 0;
            while ($i < 3) {
                $ran = rand(0, $count - 1);
                if (!in_array($data_ids[$ran], $data_list)) {
                    $data_list[] = $data_ids[$ran];
                    $i++;
                }
            }
            $map['id'] = array('in', $data_list);
            $data = $db_span->where($map)->select();
            $this->ajaxReturn($data);
        }
        $this->error('error');
    }

    public function test()
    {
        $jy = '网络';
        $db_span = M('span');
        $count = $db_span->where('status = "1" and span like "%' . $jy . '%"')->count();
        $data_span = $db_span->where('status = "1" and span like "%' . $jy . '%"')->field('id')->select();
        $data_ids = array_column($data_span, 'id');
        $data_list = [];
        $i = 0;
        while ($i < 3) {
            $ran = rand(0, $count - 1);
            if (!in_array($data_ids[$ran], $data_list)) {
                $data_list[] = $data_ids[$ran];
                $i++;
            }
        }
        $map['id'] = array('in', $data_list);
        $data = $db_span->where($map)->select();
        $this->ajaxReturn($data);
    }
}